package controller

import (
	"github.com/gin-gonic/gin"
	"go_web_template/common"
	"go_web_template/model"
	"go_web_template/response"
	"go_web_template/service"
	"net/http"
	"strconv"
	"time"
)

func SaveUser(c *gin.Context) {
	//获取参数 a b
	username := c.GetString("username")
	id := uint(c.GetFloat64("id"))
	//验证此处有没有接收到参数
	println("username", username)
	println("id", id)
	user := model.User{
		Username: username,
	}
	service.SaveUser(id, &user)
	response.Success(c, user)
}

func DelUser(c *gin.Context) {
	//获取参数 a b
	id, _ := strconv.Atoi(c.Query("id"))
	println(id)
	service.DelUser(uint(id))
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"data": nil,
	})
}

func GetUser(c *gin.Context) {
	common.Set("test", "123")
	println("test:::" + common.Get("test"))
	common.SetWithTime("test", "456", 2)
	println("test:::" + common.Get("test"))
	time.Sleep(3 * time.Second)
	println("test:::" + common.Get("test"))
	//获取参数 a b
	id, _ := strconv.Atoi(c.Query("id"))

	var user model.User
	service.GetUser(uint(id), &user)
	response.Success(c, user)
}
